//
// Created by 王乐 on 2022/2/7.
//

#include "LePlayerGameObject.h"
#include "LeRigidBody.h"
#include "LeSpriteRenderer.h"
#include "LePlayerBehaviour.h"
#include "LeParticleSystem.h"

LePlayerGameObject::LePlayerGameObject()
{}

LePlayerGameObject::~LePlayerGameObject()
{

}

void LePlayerGameObject::Initialize()
{
    strName = "MyPlayer";

    // Transform
    // ---------
    m_Transform.setPosition(6.f, 5.f, 0.0f);

    // RigidBody
    // ---------
    b2BodyDef bodyDef;
    bodyDef.userData.pointer = reinterpret_cast<uintptr_t>(this);
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(m_Transform.m_Position.x, m_Transform.m_Position.y);
    bodyDef.angle = m_Transform.m_Rotation.z;
    bodyDef.fixedRotation = true;
    bodyDef.linearDamping = 1.f;

    b2FixtureDef fixtureDef;
    b2PolygonShape polygonshape;
    polygonshape.SetAsBox(m_Transform.m_Scale.x / 2 * 0.5f, m_Transform.m_Scale.y / 2);
    fixtureDef.shape = &polygonshape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;

    LeRigidBody* pRigidBody = new LeRigidBody();
    pRigidBody->initRigidBody(bodyDef, fixtureDef);

    // Behaviour
    // ---------
    LePlayerBehaviour* pBehaviour = new LePlayerBehaviour();

    // Renderer
    // --------
    LeSpriteRenderer* pSpriteRenderer = new LeSpriteRenderer();
    pSpriteRenderer->initRenderer(this, "/Users/wangle/CLionProjects/Leste/res/shader/sprite.vs", "/Users/wangle/CLionProjects/Leste/res/shader/sprite.fs");

    SpriteSheet walk("/Users/wangle/CLionProjects/Leste/res/player_run.png", 8, 0.2f);
    SpriteSheet stand("/Users/wangle/CLionProjects/Leste/res/player_idle.png", 4, 0.2f);
    SpriteSheet jump("/Users/wangle/CLionProjects/Leste/res/player_jump.png", 1, 0.2f);
    pSpriteRenderer->addSpriteSheet("Walking", walk);
    pSpriteRenderer->addSpriteSheet("Standing", stand);
    pSpriteRenderer->addSpriteSheet("Jumping", jump);

    // ParticleSystem
    // --------------
    LeParticleSystem* pParticleSystem = new LeParticleSystem(this);

    // Initialize Components
    initComponents(pSpriteRenderer, pRigidBody, pBehaviour, pParticleSystem);
}
